home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Views / PedWindow.cc < prev   
Encoding:
C/C++ Source or Header  |  2000-06-24  |  8.2 KB  |  454 lines

  1. /*    ============
  2.  *    PedWindow.cc
  3.  *    ============
  4.  */
  5.  
  6. #include "PedestalDebugging.h"
  7.  
  8. #include <Windows.h>
  9.  
  10. #include "PedWindow.hh"
  11. #include "PedPane.hh"
  12. #include "PedAgent.hh"
  13.  
  14. PedWindow::PedWindow()
  15. : mPersistent(false), macWindow(NULL), mStorage(NULL), mProcID(documentProc)
  16. , mPane(NULL), mAgent(NULL)
  17. {
  18.     Rect bounds = {100, 200, 300, 400};
  19.     mBounds = bounds;
  20.     unsigned char *title = "\pUntitled";
  21.     ::BlockMoveData(title, mTitle, title[0] + 1);
  22. }
  23.  
  24. PedWindow::PedWindow(PedAgent *inAgent)
  25. : mPersistent(false), macWindow(NULL), mStorage(NULL), mProcID(documentProc)
  26. , mPane(NULL), mAgent(inAgent)
  27. {
  28.     inAgent->retain();
  29.     inAgent->SetWindow(this);
  30.     
  31.     Rect bounds = {100, 200, 300, 400};
  32.     mBounds = bounds;
  33.     unsigned char *title = "\pUntitled";
  34.     ::BlockMoveData(title, mTitle, title[0] + 1);
  35. }
  36.  
  37. PedWindow::~PedWindow()
  38. {
  39.     if (mAgent) {
  40.         mAgent->release();
  41.         mAgent = NULL;
  42.     }
  43.     if (mPane) {
  44.         mPane->release();
  45.         mPane = NULL;
  46.     }
  47. #if 0
  48.     if (!mDeleting) {
  49.         mDeleting = true; 
  50.         if (macWindow)
  51.             Close();
  52.     }
  53. #endif
  54.     // As long as a window is open, it should be retained,
  55.     // and we should not be destructed outside of release().
  56.     if (macWindow) {
  57.         DebugBeep();
  58.         throw;
  59.     }
  60. }
  61.  
  62. void
  63. PedWindow::Open()
  64. {
  65.     if (!macWindow) {
  66.         retain();
  67.         // TODO: Support monochrome machines
  68.         macWindow = ::NewCWindow(NULL, &mBounds, mTitle, true, mProcID, WindowPtr(-1), true, (long)this);
  69.         ((WindowPeek)macWindow)->windowKind = kPedestalWindowKind;
  70.         //Rect clip = macWindow->portRect;
  71.         //::ClipRect(&clip);
  72.     }
  73.     Focus();
  74.     Select();
  75.     if (mPane)
  76.         mPane->Open();
  77. }
  78.  
  79. void
  80. PedWindow::Open(const Rect &inBounds, const Str255 inTitle)
  81. {
  82.     SetBounds(inBounds);
  83.     SetTitle(inTitle);
  84.     Open();
  85. }
  86.  
  87. void
  88. PedWindow::Close()
  89. {
  90.     if (mPane)
  91.         mPane->Close();
  92.     if (macWindow) {
  93.         if (mStorage) {
  94.             ::CloseWindow(macWindow);
  95.             // delete mStorage;
  96.             // mStorage = NULL;
  97.         } else
  98.             ::DisposeWindow(macWindow);
  99.         macWindow = NULL;
  100.         release();
  101.     }
  102. }
  103.  
  104. void
  105. PedWindow::Dispose()
  106. {
  107.     retain();  // Not sure if this is necessary, but it can't hurt
  108.     Close();
  109.     if (mPane) {
  110.         mPane->Dispose();
  111.         SetPane(NULL);
  112.     }
  113.     if (mAgent) mAgent->release();
  114.     mAgent = NULL;
  115.     release();
  116. }
  117.  
  118. PedPane *
  119. PedWindow::Pane()
  120. {
  121.     return mPane;
  122. }
  123.  
  124. void 
  125. PedWindow::SetPane(PedPane *inPane)
  126. {
  127.     PedPane *oldPane = mPane;
  128.     if (inPane) inPane->retain();
  129.     mPane = inPane;
  130.     if (oldPane) oldPane->release();
  131.     
  132.     if (mPane)
  133.         mPane->Resize(mBounds.right - mBounds.left, mBounds.bottom - mBounds.top);
  134. }
  135.  
  136. void
  137. PedWindow::Focus()
  138. {
  139.     if (macWindow)
  140.         ::SetPort(macWindow);
  141. }
  142.  
  143. void
  144. PedWindow::Activate()
  145. {
  146.     Focus();
  147.     
  148.     if (macWindow) {
  149.         Rect invalid;
  150.         invalid = macWindow->portRect;
  151.         invalid.top = invalid.bottom - 15;
  152.         ::InvalRect(&invalid);
  153.         
  154.         invalid = macWindow->portRect;
  155.         invalid.left = invalid.right - 15;
  156.         invalid.bottom = invalid.bottom - 15;
  157.         ::InvalRect(&invalid);
  158.     }
  159.     
  160.     if (mPane)
  161.         mPane->Activate();
  162. }
  163.  
  164. void
  165. PedWindow::Deactivate()
  166. {
  167.     Focus();
  168.     
  169.     if (macWindow) {
  170.         Rect invalid;
  171.         invalid = macWindow->portRect;
  172.         invalid.top = invalid.bottom - 15;
  173.         ::InvalRect(&invalid);
  174.         
  175.         invalid = macWindow->portRect;
  176.         invalid.left = invalid.right - 15;
  177.         invalid.bottom = invalid.bottom - 15;
  178.         ::InvalRect(&invalid);
  179.     }
  180.     
  181.     if (mPane)
  182.         mPane->Deactivate();
  183. }
  184.  
  185. void
  186. PedWindow::Refresh()
  187. {
  188.     if (macWindow) {
  189.         ::SetPort(macWindow);
  190.         ::InvalRect(&macWindow->portRect);
  191.     }
  192. }
  193.  
  194. void
  195. PedWindow::Update()
  196. {
  197.     if (!macWindow) return;
  198.     
  199.     ::SetPort(macWindow);
  200.     ::BeginUpdate(macWindow);
  201.     //DrawContent();
  202.         Rect clip = macWindow->portRect;
  203.         ::ClipRect(&clip);
  204.         if (mPane) {
  205.             ::EraseRect(&macWindow->portRect);
  206.             ::DrawGrowIcon(macWindow);
  207.             clip.right -= 15;
  208.             clip.bottom -= 15;
  209.             //::ClipRect(&clip);
  210.             mPane->DrawContent();
  211.         } else {
  212.             // This will flicker, but we're just pointing out paneless windows.
  213.             ::PaintRect(&macWindow->portRect);
  214.             clip.left = clip.right - 15;
  215.             clip.top = clip.bottom - 15;
  216.             //::ClipRect(&clip);
  217.             ::DrawGrowIcon(macWindow);
  218.             clip = macWindow->portRect;
  219.             //::ClipRect(&clip);
  220.         }
  221.     ::EndUpdate(macWindow);
  222. }
  223.  
  224. #if 0
  225. void
  226. PedWindow::DrawContent()
  227. {
  228.     if (macWindow) {
  229.         Rect clip = macWindow->portRect;
  230.         ::ClipRect(&clip);
  231.         ::EraseRect(&macWindow->portRect);
  232.         ::DrawGrowIcon(macWindow);
  233.         clip.right -= 15;
  234.         clip.bottom -= 15;
  235.         ::ClipRect(&clip);
  236.         if (mPane)
  237.             mPane->DrawContent();
  238.         }
  239.     }
  240. }
  241. #endif
  242.  
  243. void
  244. PedWindow::Select()
  245. {
  246.     if (!macWindow) return;
  247.     
  248.     ::SelectWindow(macWindow);
  249. }
  250.  
  251. void
  252. PedWindow::Resize(short inWidth, short inHeight)
  253. {
  254.     short oldWidth, oldHeight;
  255.     Rect rect, invalid;
  256.     
  257.     if (macWindow) {
  258.         rect = macWindow->portRect;
  259.         oldWidth = rect.right - rect.left;
  260.         oldHeight = rect.bottom - rect.top;
  261.         ::SizeWindow(macWindow, inWidth, inHeight, true);
  262.         ::SetPort(macWindow);
  263.         
  264.         mBounds.right = mBounds.left + inWidth;
  265.         mBounds.bottom = mBounds.top + inHeight;
  266.         
  267.         // Invalidate old scrollbar regions if expanding;
  268.         // invalidate new scrollbar regions if shrinking.
  269.         if (inWidth > oldWidth) {
  270.             invalid = rect;
  271.             invalid.left = invalid.right - 15;
  272.             ::InvalRect(&invalid);
  273.         } else if (inWidth < oldWidth) {
  274.             invalid = macWindow->portRect;
  275.             invalid.left = invalid.right - 15;
  276.             ::InvalRect(&invalid);
  277.         }
  278.         if (inHeight > oldHeight) {
  279.             invalid = rect;
  280.             invalid.top = invalid.bottom - 15;
  281.             ::InvalRect(&invalid);
  282.         } else if (inHeight < oldHeight) {
  283.             invalid = macWindow->portRect;
  284.             invalid.top = invalid.bottom - 15;
  285.             ::InvalRect(&invalid);
  286.         }
  287.         
  288.         if (mPane)
  289.             mPane->Resize(inWidth, inHeight);
  290.     }
  291. }
  292.  
  293. #if 0
  294. void
  295. PedWindow::Resize(short inWidth, short inHeight)
  296. {
  297.     short oldWidth, oldHeight;
  298.     Rect rect, invalid;
  299.     
  300.     if (macWindow) {
  301.         rect = macWindow->portRect;
  302.         oldWidth = rect.right - rect.left;
  303.         oldHeight = rect.bottom - rect.top;
  304.         ::SizeWindow(macWindow, inWidth, inHeight, true);
  305.         ::SetPort(macWindow);
  306.         
  307.         mBounds.right = mBounds.left + inWidth;
  308.         mBounds.bottom = mBounds.top + inHeight;
  309.         
  310.         // Invalidate old scrollbar regions if expanding;
  311.         // invalidate new scrollbar regions if shrinking.
  312.         if (inWidth > oldWidth) {
  313.             invalid = rect;
  314.             invalid.left = invalid.right - 15;
  315.             ::InvalRect(&invalid);
  316.         } else if (inWidth < oldWidth) {
  317.             invalid = macWindow->portRect;
  318.             invalid.left = invalid.right - 15;
  319.             ::InvalRect(&invalid);
  320.         }
  321.         if (inHeight > oldHeight) {
  322.             invalid = rect;
  323.             invalid.top = invalid.bottom - 15;
  324.             ::InvalRect(&invalid);
  325.         } else if (inHeight < oldHeight) {
  326.             invalid = macWindow->portRect;
  327.             invalid.top = invalid.bottom - 15;
  328.             ::InvalRect(&invalid);
  329.         }
  330.         
  331.         if (mPane)
  332.             mPane->Resize(inWidth - 15, inHeight - 15);
  333.     }
  334. }
  335. #endif
  336.  
  337. void
  338. PedWindow::GetFrame(Rect &outFrame)
  339. {
  340.     if (macWindow) {
  341.         outFrame = macWindow->portRect;
  342.     } else {
  343.         ::SetRect(&outFrame, 0, 0, 
  344.             mBounds.right  - mBounds.left, 
  345.             mBounds.bottom - mBounds.top );
  346.     }
  347. }
  348.  
  349. void
  350. PedWindow::GetBounds(Rect &outBounds)
  351. {
  352.     outBounds = mBounds;
  353. }
  354.  
  355. void
  356. PedWindow::SetBounds(const Rect &inBounds)
  357. {
  358.     mBounds = inBounds;
  359. }
  360.  
  361. void
  362. PedWindow::GetTitle(Str255 outItemText)
  363. {
  364.     if (macWindow)
  365.         ::GetWTitle(macWindow, outItemText);
  366.     else
  367.         ::BlockMoveData(mTitle, outItemText, mTitle[0]);
  368. }
  369.  
  370. void
  371. PedWindow::SetTitle(const Str255 inItemText)
  372. {
  373.     ::BlockMoveData(inItemText, mTitle, inItemText[0] + 1);
  374.     if (macWindow)
  375.         ::SetWTitle(macWindow, inItemText);
  376. }
  377.  
  378. PedAgent *
  379. PedWindow::Agent()
  380. {
  381.     return mAgent;
  382. }
  383.  
  384. void
  385. PedWindow::SetAgent(PedAgent *inAgent)
  386. {
  387.     if (inAgent)
  388.         inAgent->retain();
  389.     PedAgent *oldAgent = mAgent;
  390.     mAgent = inAgent;
  391.     if (oldAgent)
  392.         oldAgent->release();
  393. }
  394.  
  395. void
  396. PedWindow::DispatchNullEvent(EventRecord &inEvent)
  397. {
  398.     if (mPane)
  399.         mPane->DispatchNullEvent(inEvent);
  400. }
  401.  
  402. void
  403. PedWindow::DispatchClickEvent(EventRecord &inEvent)
  404. {
  405.     if (!macWindow) throw;
  406.     
  407.     if (macWindow != ::FrontWindow())
  408.         ::SelectWindow(macWindow);
  409.     else {
  410.         ::SetPort(macWindow);
  411.         if (mPane)
  412.             mPane->DispatchClickEvent(inEvent);
  413.         else {
  414.             Rect dot;
  415.             
  416.             ::SetPort(macWindow);
  417.             ::GlobalToLocal(&inEvent.where);
  418.             dot.top = inEvent.where.v -2;
  419.             dot.left = inEvent.where.h - 2;
  420.             dot.bottom = inEvent.where.v + 2;
  421.             dot.right = inEvent.where.h + 2;
  422.             ::InvertOval(&dot);
  423.         }
  424.     }
  425. }
  426.  
  427. void
  428. PedWindow::DispatchKey(EventRecord &inEvent)
  429. {
  430.     if (mAgent)
  431.         mAgent->ProcessKey(inEvent);
  432.     else if (mPane)
  433.         mPane->DispatchKey(inEvent);
  434. }
  435.  
  436. void
  437. PedWindow::ProcessGoAway(EventRecord &inEvent)
  438. {
  439.     ::SetPort(macWindow);
  440.     if (::TrackGoAway(macWindow, inEvent.where)) {
  441.         HandleClose();
  442.     }
  443. }
  444.  
  445. void
  446. PedWindow::HandleClose()
  447. {
  448.     if (mAgent) {
  449.         mAgent->CloseWindow();
  450.     } else {
  451.         Close();
  452.     }
  453. }
  454.